var express = require('express');
var router = express.Router();
var http = require('http');

router.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By",' 3.2.1')
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});


// 轮播的接口    localhost:3000/lunbo?time=1495519120152
router.get('/lunbo', function(req, res) {
    // 在nodejs 里面 我们要去使用 http 模板去访问  卖座网的接口
    var t = req.query.time;

    var url = 'http://m.maizuo.com/v4/api/billboard/home?__t=' + t;

    http.get(url, function(response) {
        var data = '';
        response.on('data', function(chunk) {
            data += chunk;
        })

        response.on('end', function() {
            res.send(data);
        })
    });
})

// 正在热映的接口    http://m.maizuo.com/v4/api/film/now-playing?__t=1495518126864&page=1&count=5
router.get('/playing', function(req, res) {
    // 在nodejs 里面 我们要去使用 http 模板去访问  卖座网的接口
    var t = req.query.time;

    var url = 'http://m.maizuo.com/v4/api/film/now-playing?__t=' + t +'&page=1&count=5';

    http.get(url, function(response) {
        var data = '';
        response.on('data', function(chunk) {
            data += chunk;
        })

        response.on('end', function() {
            res.send(data);
        })
    });
})

// 即将上映的接口   http://m.maizuo.com/v4/api/film/coming-soon?__t=1495518126867&page=1&count=3
router.get('/coming', function(req, res) {
    // 在nodejs 里面 我们要去使用 http 模板去访问  卖座网的接口
    var t = req.query.time;

    var url = 'http://m.maizuo.com/v4/api/film/coming-soon?__t=' + t +'&page=1&count=3';

    http.get(url, function(response) {
        var data = '';
        response.on('data', function(chunk) {
            data += chunk;
        })

        response.on('end', function() {
            
            res.send(data);
        })
    });
})

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

module.exports = router;
